package com.resou.reader.discovery.comment;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.resou.reader.R;
import com.resou.reader.api.entry.BookListCommentBean;
import com.resou.reader.api.entry.CommentBean;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.discovery.comment.adapter.CommentAdapter;
import com.resou.reader.utils.CountDown;
import com.resou.reader.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 评论详情
 */
public class CommentActivity extends ResouBaseActivity<CommentPresenter> implements ICommentContract.ICommentView, OnRefreshLoadMoreListener {
    /**
     * 进入评论的方法
     *
     * @param context
     */
    public static void start(Context context, String bookId) {
        Intent starter = new Intent(context, CommentActivity.class);
        starter.putExtra("bookId", bookId);
        context.startActivity(starter);
    }


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    @BindView(R.id.tvAddComment)
    TextView tvAddComment;
    @BindView(R.id.rlContent)
    RelativeLayout rlContent;
    @BindView(R.id.ivRemindUser)
    ImageView ivRemindUser;
    CommentAdapter commentAdapter;
    TextView tvAllNum;
    int page = 0;
    String bookId;
    CountDownTimer countDownTimer;

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        mPresenter = new CommentPresenter(this);
    }

    @Override
    protected void initView() {
        tvTitle.setText("评论");
        commentAdapter = new CommentAdapter(R.layout.adapter_comment, null);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.adapter_commentheader, null);
        tvAllNum = view.findViewById(R.id.tvCommentAllNum);
        commentAdapter.setHeaderView(view);
        mSmartRefreshLayout.setOnRefreshLoadMoreListener(this);
        mRecyclerView.setAdapter(commentAdapter);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_comment;
    }

    @Override
    public void retryRefreshData() {
        page = 0;
        mPresenter.quearCommentData(bookId, page);
    }

    @Override
    public void initData() {
        bookId = getIntent().getStringExtra("bookId");
        page = 0;
        mPresenter.quearCommentData(bookId, page);
        countDownTimer = new CountDownTimer(2000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                if (ivRemindUser != null) {
                    ivRemindUser.startAnimation(AnimationUtils.loadAnimation(CommentActivity.this, R.anim.fade_out));
                    ivRemindUser.setVisibility(View.GONE);
                }
            }
        };
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    /**
     * 错误提示
     *
     * @param throwable
     */
    @Override
    public void showError(Throwable throwable) {
        dissLoading();
        ToastUtil.makeShortToast("网络异常");
        if (page == 0) {
            showNetErrorView(R.id.rlContent);
        }
    }

    /**
     * 显示空页面
     */
    @Override
    public void showEmpty() {
        commentAdapter.setHeaderAndEmpty(true);
        mSmartRefreshLayout.finishRefresh(true);
        commentAdapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.empty_comment, null));
    }

    /**
     * 显示评论数据
     *
     * @param list
     */
    @Override
    public void showCommentData(List<BookListCommentBean.CommentsBean> list, int total) {
        tvAllNum.setText("全部（" + total + ")");
        if (page == 0) {
            mSmartRefreshLayout.finishRefresh(true);
            commentAdapter.setNewData(list);
        } else {
            commentAdapter.addData(list);
        }
    }

    /**
     * 点赞
     * //TODO 目前此功能不做
     */
    @Override
    public void showPraiseComment() {

    }

    /**
     * 取消点赞
     * //TODO 目前此功能不做
     */
    @Override
    public void showUnPraiseComment(String bookId) {

    }

    @Override
    public SmartRefreshLayout getSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }


    @OnClick({R.id.ivBack, R.id.rlContent1, R.id.ivRemindUser})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.rlContent1:
                if (isNetwork()) {
                    ivRemindUser.setVisibility(View.VISIBLE);
                    countDownTimer.start();
                }
                break;
            case R.id.ivRemindUser:
                ivRemindUser.setVisibility(View.GONE);
                countDownTimer.cancel();
                break;
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        mPresenter.quearCommentData(bookId, page);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 0;
        mPresenter.quearCommentData(bookId, page);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer.onFinish();
        }
    }
}
